package main

import (
	"github.com/bgmp/config"
	"github.com/bgmp/corn/job"
	"github.com/robfig/cron/v3"
	log "github.com/sirupsen/logrus"
	"time"
)

const USER_ID = 6

func onceJob() {
	current := time.Now()
	log.Infof("%s 尝试更新", current)

	for i := 1; i <= 5; i++ {
		start, end := job.GetTime(current, i)
		log.Infof("更新 %s 的数据", end)
		job.RefreshBgs(USER_ID, start, end)
		job.RefreshDiets(USER_ID, start, end)
		job.RefreshHearts(USER_ID, start, end)
		job.RefreshExercise(USER_ID, start, end)
	}

	log.Info("更新完成\n\n")
}

func cornJob() {
	log.Info("开始执行定时任务")
	c := cron.New()

	onceJob()

	_, err := c.AddFunc("0 4 * * *", func() {
		onceJob()
	})

	if err != nil {
		log.Info("启动失败", err)
		return
	}
	c.Run()
}

func initJob() {
	t := time.Now()

	t = time.Date(2024, 1, 29, 10, 0, 0, 0, t.Location())
	start, end := job.GetTime(t, 0)
	job.RefreshBgs(USER_ID, start, end)

	t = time.Date(2024, 6, 17, 10, 0, 0, 0, t.Location())
	start, end = job.GetTime(t, 0)
	job.RefreshDiets(USER_ID, start, end)
}

func main() {
	config.Init()

	cornJob()

	//c := cron.New(cron.WithSeconds())
	//_, err := c.AddFunc("*/2 * * * * *", func() { fmt.Println("Every hour on the half hour") })
	//if err != nil {
	//	fmt.Println(err)
	//}
	//c.Run()

	select {}
}
